<?php
abstract class DatabaseRecord
{
	
	public    $primary_key;
	public    $columns;
	public    $isDirty = false;
	public    $storageEngine;
	
	public $isNew     = true;
	
	abstract function initWithColumns();
	
	// just a convenience method
	// strictly an alias of __destruct.
	public function release()
	{
		$this->__destruct();
	}
	
	final public function __construct()
	{
		$this->initWithColumns();
	}
	
	public function __destruct()
	{
		foreach($this->columns as $key=>$value)
		{
			$this->columns[$key]->__destruct();
		}
	}
	
	public function __set($property, $value)
	{
		/* 
			[2007-08-10]
			WARNING MULTI-PRIMARY KEYS CAN BE SET, BUT WILL NOT BE HANDLED PROPERLY 
			THROUGH THE REST OF THE SYSTEM.  THIS FUNCTIONALITY NEEDS TO BE CREATED TO 
			WORK SYSTEM WIDE 
		*/
		
		if (!$this->columns)
			$this->columns = array();
			//$this->columns = new Object();
			
		//if (isset($this->record->$property))
		
		/* for filters we are doing this as oppoosed to a simple public 
		   property because we want to be sure that ONLY a SQLFilterCollection
		   can be set, and nothing else
		*/
		/*if ($property == 'filters')
		{
			try 
			{
				if ($value instanceof SQLFilterCollection || $value == null)
					$this->SQLFilterCollection = $value;
				else
					throw new Exception("Error: Object->filters must be of type SQLFilterCollection.");
			}
			catch (Exception $e)
			{
				echo $e->getMessage();
			}
		}
		else if ($property == 'sort')
		{
			try 
			{
				if ($value instanceof SQLSortCollection || $value == null)
					$this->SQLSortCollection = $value;
				else
					throw new Exception("Error: Object->sort must be of type SQLSortCollection.");
			}
			catch (Exception $e)
			{
				echo $e->getMessage();
			}
		}*/
		//else
		//{
			//echo $value,"<br>";
			if ($value instanceof DatabaseColumn)
			{
				$value->column = $property;
				$this->columns[$property] = $value;
				//$this->columns->$property = $value;
			
				if ($value instanceof PrimaryKey)
				{
					if (empty($this->primary_key))
					{
						$this->primary_key = &$value;
					}
					else
					{
						if (is_array($this->primary_key))
							$this->primary_key[] = &$value;
						else
							$this->primary_key = array($this->primary_key, &$value);
					}
				}
			}
			else
			{	
				//if ($this->columns->$property instanceof ForeignKey)
				if ($this->columns[$property] instanceof ForeignKey)
				{
					//if ($value instanceof $this->columns->$property->relation)
					if ($value instanceof $this->columns[$property]->relation)
					{
						if ($value->primary_key->value)
						{
							$this->columns[$property]->userDefinedValue    = $value->primary_key->value;
							$this->columns[$property]->relation = $value;
							
							//$this->columns->$property->userDefinedValue    = $value->primary_key->value;
							//$this->columns->$property->relation = $value;
						}
						else
						{
							$this->columns[$property]->add($value);
							//$this->columns->$property->add($value);
							
							//$value->save();
							//$this->columns->$property->value    = $value->primary_key->value;
							//$this->columns->$property->relation = $value;
						}
					} 
					else
					{
						$this->columns[$property]->userDefinedValue = $value;
						//$this->columns->$property->userDefinedValue = $value;
					}
				}
				else
				{
					$this->columns[$property]->userDefinedValue = $value;
					//$this->columns->$property->userDefinedValue = $value;
					//echo $property.": ".$this->record->$property->value;
				}
			}
		//}
	}
	
	public function __get($property)
	{	
		if ($property == 'isNew')
		{
			return $this->isNew;
		}
		else
		{
			//if (isset($this->columns->$property)) 
			if (isset($this->columns[$property])) 
			{
				//if ($this->columns->$property instanceof ForeignKey):
				if ($this->columns[$property] instanceof ForeignKey):
					return $this->columns[$property];
				//elseif($this->columns->$property instanceof ManyToMany):
				elseif($this->columns[$property] instanceof ManyToMany):
					return $this->columns[$property];
				//elseif($this->columns->$property instanceof SerializedPHPObject):
				elseif($this->columns[$property] instanceof SerializedPHPObject):
					return $this->columns[$property]->userDefinedValue;
				else:
					return $this->columns[$property]->value;
				endif;
			}
			else
			{	
				return false;
			}
		}
	}
	
	
	public function __toString()
	{
		return "[phorm ".get_class($this)."]";
	}
}
?>